import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_fai_webview/flutter_fai_webview.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../../AppConfig/Config.dart';

class FrequentlyAQ extends StatefulWidget {
  const FrequentlyAQ({super.key});

  @override
  State<FrequentlyAQ> createState() => _FrequentlyAQState();
}

class _FrequentlyAQState extends State<FrequentlyAQ> {
  late PullToRefreshController pullToRefreshController;
  InAppWebViewController? webViewController;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    pullToRefreshController =PullToRefreshController(
        options: PullToRefreshOptions(
          color: Colors.blue,
        ),
        onRefresh: ()async{
          if(Platform.isAndroid){
            webViewController?.reload();
          }else if(Platform.isIOS){
            webViewController?.loadUrl(urlRequest: URLRequest(
                url: await webViewController?.getUrl()
            ));
          }
        }
    );
  }
  ///加载 Html 的回调
  ///[code]消息类型标识
  ///[msg] 消息内容
  ///[content] 回传的参数
  void callBack(int ?code, String ?msg, content) {
    //加载页面完成后 对页面重新测量的回调
    //这里没有使用到
    //当FaiWebViewWidget 被嵌套在可滑动的 widget 中，必须设置 FaiWebViewWidget 的高度
    //设置 FaiWebViewWidget 的高度 可通过在 FaiWebViewWidget 嵌套一层 Container 或者 SizeBox
    if (code == 201) {
      //页面加载完成后 测量的 WebView 高度
      double webViewHeight = content;
      print("webViewHeight " + webViewHeight.toString());
    } else {
      //其他回调
    }
    setState(() {
      print("回调：code[" + code.toString() + "]; msg[" + msg.toString() + "]");
    });
  }
  String url = "";
  double progress = 0;
  Widget build(BuildContext context) {
    final screenSize = MediaQuery.of(context).size;
    return Scaffold(
      backgroundColor:Colors.white,
      appBar: AppBar(
        centerTitle: true,
        backgroundColor: Colors.white,
        title: Text("常见问题",style: TextStyle(color: Colors.black,fontSize: 16.sp,fontWeight: FontWeight.w300),),
      ),
      body: ListView(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text("货损情况怎么办？",style: TextStyle(color: Colors.black,fontSize: 18.sp,fontWeight: FontWeight.w600),),
            ],
          ),
          SizedBox(height: 10.h,),
          Container(
            margin: EdgeInsets.only(left: 10.w,right: 10.w),
            width: screenSize.width,
            child: Text("1、首先明确货损/货少是否是司机责任导致的，保留货在司机车上损坏的照片视频，司机承认货损/货少是自己原因的聊天凭证录音等;\n2、需要司机赔偿需提供货物价值清单、厂家索赔函材料证明损失金额，若没有证明，无法邀请司机赔偿；\n3、协商未果，运单点击投诉，上传凭证，客服介入调解。"
            ,style: TextStyle(color: MyColors.textColor,fontSize: 16.sp),),
          ),
          SizedBox(height: 16.h,),
          Container(
            padding: EdgeInsets.only(left: 10.w,right: 10.w),
            child: RichText(
              text: TextSpan(
                text: '详情请点击 ', // 第一部分文本
                style: TextStyle(color: MyColors.textColor,fontSize: 16.sp),
                children: [

                  TextSpan(
                    text: '《速配货平台用户违规行为处理规则》', // 第二部分文本
                    style: TextStyle(color: MyColors.blueColor,fontSize: 16.sp),
                    recognizer: TapGestureRecognizer()..onTap = () {

                    },
                  ),
                ],
              ),
            ),
          ),
          SizedBox(height: 16.h,),
          Container(
            margin: EdgeInsets.only(left: 10.w,right: 10.w),
            height: screenSize.height/2,
            child:  InAppWebView(
              initialUrlRequest: URLRequest(url: Uri.parse("https://www.baidu.com")),
              initialOptions: InAppWebViewGroupOptions(
                crossPlatform: InAppWebViewOptions(
                    clearCache: true,
                    useShouldOverrideUrlLoading: true,
                    mediaPlaybackRequiresUserGesture: false
                ),
                android: AndroidInAppWebViewOptions(useHybridComposition: true),
                ios: IOSInAppWebViewOptions(allowsInlineMediaPlayback: true),
              ),
              pullToRefreshController: pullToRefreshController,
              onWebViewCreated: (controller){
                webViewController = controller;
              },
              onPageCommitVisible: (con,uri){
                con.goBack();
              },
              androidOnPermissionRequest: (controller,origin,resources)async{
                return PermissionRequestResponse(
                    resources: resources,
                    action: PermissionRequestResponseAction.GRANT
                );
              },
              onLoadStop: (controller,url)async{
                pullToRefreshController.endRefreshing();
              },
              onLoadError: (controller,url,code,message){
                pullToRefreshController.endRefreshing();
              },
            )
          )
        ],
      ),
    );
  }
}
